from variable import *
import datetime

def minNums(startTime, endTime):
    '''计算两个时间点之间的分钟数'''
    # 处理格式,加上秒位
    startTime1 = startTime
    endTime1 = endTime
    # 计算分钟数
    startTime2 = datetime.datetime.strptime(startTime1, "%Y-%m-%d %H:%M:%S")
    endTime2 = datetime.datetime.strptime(endTime1, "%Y-%m-%d %H:%M:%S")
    seconds = (endTime2 - startTime2).seconds
    # 来获取时间差中的秒数。注意，seconds获得的秒只是时间差中的小时、分钟和秒部分的和，并没有包含时间差的天数（既是两个时间点不是同一天，失效）
    total_seconds = (endTime2 - startTime2).total_seconds()
    # 来获取准确的时间差，并将时间差转换为秒
    mins = total_seconds / 60
    return int(mins)

def WriteServiceReportBefore():

    wb = openpyxl.load_workbook(MailWork_path + "\\" + ServiceReport_NameBefore, data_only=True)

    sheet1_ServiceBefore = wb['术前']

    sheet1_ServiceBefore['B4'] = OrderNumber
    sheet1_ServiceBefore['H4'] = PIUSerialNumber
    sheet1_ServiceBefore['E6'] = Hospital
    sheet1_ServiceBefore['H6'] = HospitalAddress
    sheet1_ServiceBefore['H8'] = ServiceTimeBefore

    sheet1_ServiceBefore['A26'] = datetime.datetime.strptime(ATPTestTimeStart,"%H:%M")
    sheet1_ServiceBefore['B26'] = datetime.datetime.strptime(ATPTestTimeStop,"%H:%M")
    sheet1_ServiceBefore['B25'] = sheet1_ServiceBefore['A26'].value
    sheet1_ServiceBefore['A25'] = sheet1_ServiceBefore['B25'].value - datetime.timedelta(minutes=10)
    sheet1_ServiceBefore['B24'] = sheet1_ServiceBefore['A25'].value
    sheet1_ServiceBefore['A24'] = sheet1_ServiceBefore['B24'].value - datetime.timedelta(minutes=10)
    sheet1_ServiceBefore['A27'] = sheet1_ServiceBefore['B26'].value
    sheet1_ServiceBefore['B27'] = sheet1_ServiceBefore['A27'].value + datetime.timedelta(minutes=20)

    sheet1_ServiceBefore["G24"] = minNums(str(sheet1_ServiceBefore['A24'].value), str(sheet1_ServiceBefore['B24'].value))
    sheet1_ServiceBefore["G25"] = minNums(str(sheet1_ServiceBefore['A25'].value), str(sheet1_ServiceBefore['B25'].value))
    sheet1_ServiceBefore["G26"] = minNums(str(sheet1_ServiceBefore['A26'].value), str(sheet1_ServiceBefore['B26'].value))
    sheet1_ServiceBefore["G27"] = minNums(str(sheet1_ServiceBefore['A27'].value), str(sheet1_ServiceBefore['B27'].value))
    sheet1_ServiceBefore["G28"] = minNums(str(sheet1_ServiceBefore['A24'].value), str(sheet1_ServiceBefore['B27'].value))

    wb.save(ServiceReport_NameBefore)


def WriteServiceReportAfter():
    wb = openpyxl.load_workbook(MailWork_path + "\\" + ServiceReport_NameAfter, data_only=True)

    sheet1_ServiceAfter = wb['术后']

    sheet1_ServiceAfter['B4'] = OrderNumber
    sheet1_ServiceAfter['H4'] = PIUSerialNumber
    sheet1_ServiceAfter['E6'] = Hospital
    sheet1_ServiceAfter['H6'] = HospitalAddress
    sheet1_ServiceAfter['H8'] = ServiceTimeAfter

    sheet1_ServiceAfter['A21'] = datetime.datetime.strptime(CleanTimeStart, "%H:%M")
    sheet1_ServiceAfter['B21'] = datetime.datetime.strptime(CleanTimeStop, "%H:%M")
    sheet1_ServiceAfter['A22'] = sheet1_ServiceAfter['B21'].value
    sheet1_ServiceAfter['B22'] = sheet1_ServiceAfter['A22'].value + datetime.timedelta(minutes=10)
    sheet1_ServiceAfter['A23'] = sheet1_ServiceAfter['B22'].value
    sheet1_ServiceAfter['B23'] = sheet1_ServiceAfter['A23'].value + datetime.timedelta(minutes=10)
    sheet1_ServiceAfter['A24'] = sheet1_ServiceAfter['B23'].value
    sheet1_ServiceAfter['B24'] = sheet1_ServiceAfter['A24'].value + datetime.timedelta(minutes=40)

    sheet1_ServiceAfter["G21"] = minNums(str(sheet1_ServiceAfter['A21'].value),
                                          str(sheet1_ServiceAfter['B21'].value))
    sheet1_ServiceAfter["G22"] = minNums(str(sheet1_ServiceAfter['A22'].value),
                                          str(sheet1_ServiceAfter['B22'].value))
    sheet1_ServiceAfter["G23"] = minNums(str(sheet1_ServiceAfter['A23'].value),
                                          str(sheet1_ServiceAfter['B23'].value))
    sheet1_ServiceAfter["G24"] = minNums(str(sheet1_ServiceAfter['A24'].value),
                                          str(sheet1_ServiceAfter['B24'].value))
    sheet1_ServiceAfter["G25"] = minNums(str(sheet1_ServiceAfter['A21'].value),
                                          str(sheet1_ServiceAfter['B24'].value))

    wb.save(ServiceReport_NameAfter)

